package com.wzh.musichome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.entity.dto.CollectDTO;
import com.wzh.musichome.entity.dto.CommentDTO;
import com.wzh.musichome.entity.dto.DeleteCollectDTO;
import com.wzh.musichome.entity.pojo.Collect;
import com.wzh.musichome.entity.pojo.Comment;
import com.wzh.musichome.service.CollectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author wzh
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;


    @PostMapping("/add")
    public Object addComment(@RequestBody CollectDTO collectDTO) {
        //保存到收藏的对象中
        Collect collect = new Collect();
        BeanUtils.copyProperties(collectDTO, collect);
        try {
            boolean flag = collectService.save(collect);
            if (flag) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("收藏成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "收藏失败！");
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "收藏失败！！ 具体原因" + e.getMessage());
        }
    }


    /**
     * 删除收藏
     */
    @PostMapping("/delete")
    public Object deleteCollect(@RequestBody DeleteCollectDTO deleteCollectDTO) {

       int userId = deleteCollectDTO.getUserId();
       int songId = deleteCollectDTO.getSongId();
        System.out.println(userId);
        System.out.println(songId);
        try {
            QueryWrapper<Collect> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId).eq("song_id", songId);
            boolean b = collectService.remove(wrapper);
            if (b) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("删除成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_DELETE, "删除失败！！");
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_DELETE, "删除失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 查询所有收藏
     */
    @GetMapping("/allCollect")
    public Object allCollect() {
        try {
            List<Collect> list = collectService.list();
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查找成功");
            data.setData(list);
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查找失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 查询某个用户的收藏列表
     */
    @RequestMapping(value = "/collectOfUserId", method = RequestMethod.GET)
    public Object collectOfUserId(@RequestParam int userId) {
        try {
            QueryWrapper<Collect> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            List<Collect> list = collectService.list(wrapper);
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查找成功");
            data.setData(list);
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查找失败！！ 具体原因" + e.getMessage());
        }
    }


}
